package org.homeorganiser.flex.manager;

import org.homeorganiser.flex.dao.UserDAO;
import org.homeorganiser.flex.dto.User;
import org.homeorganiser.flex.util.Constant;
import org.homeorganiser.flex.vo.LoggedInUser;
import org.homeorganiser.flex.vo.UserDetail;

public class UserManager implements IUserManager {

	UserDAO usrdao;
	/**
	 * @return the usrdao
	 */
	public UserDAO getUsrdao() {
		return usrdao;
	}
	/**
	 * @param usrdao the usrdao to set
	 */
	public void setUsrdao(UserDAO usrdao) {
		this.usrdao = usrdao;
	}
	public LoggedInUser checkUserlogin(String userName, String password) {
		LoggedInUser loggedInUsr = new LoggedInUser();
		String usrMessage        = usrdao.validateUser(userName, password);
		if(usrMessage.equals(Constant.MESSAGE_VALID_LOGIN)){
			loggedInUsr.setValidated(true);
			loggedInUsr.setUserName(userName);
			loggedInUsr.setUsrMessage("Welcome "+userName);
		}else{
			loggedInUsr.setUsrMessage(usrMessage);
		}
		return loggedInUsr;
	}
  
	public Boolean checkUserNameAvailiablity(String userName){
		boolean isavail = usrdao.checkUserName(userName);
		if(isavail){
			return isavail;
		}
		return isavail;
	}
	
	public String registerUser(UserDetail usrDetail){
		User dbusr  = new User();
		User addedUser;
		if(!checkUserNameAvailiablity(usrDetail.getUserName())){
			return "User Name not Availiable";
		}
		dbusr.setUserId(usrDetail.getUserName());
		dbusr.setPassword(usrDetail.getPassword());
		dbusr.setFirstName(usrDetail.getFirstName());
		dbusr.setLastName(usrDetail.getLastName());
		dbusr.setAddress(usrDetail.getAddress());
		dbusr.setCity(usrDetail.getCity());
		dbusr.setState(usrDetail.getState());
		dbusr.setZip( usrDetail.getZip());
		dbusr.setPhone(usrDetail.getPhone());
		dbusr.setEmail(usrDetail.getEmail());
		dbusr.setSecurityQuestion(usrDetail.getSecurityQuestion());
		dbusr.setSecurityAnswer(usrDetail.getSecurityAnswer());
	
		addedUser = usrdao.create(dbusr);
		if(addedUser != null){
			return "User Added Successfully";
		}
		return "User Addition Failure";
	}
	
}
